package xyz.chenyongpeng.orderconsumerdubbo.controller;

import ch.qos.logback.classic.gaffer.PropertyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xyz.chenyongpeng.entity.ZsCouponIncludeGoods;
import xyz.chenyongpeng.orderconsumerdubbo.service.CouponService;
import xyz.chenyongpeng.orderconsumerdubbo.service.UserService;
import xyz.chenyongpeng.orderconsumerdubbo.vo.MyCouponInfo;
import xyz.chenyongpeng.to.CouponTo;
import xyz.chenyongpeng.to.MyCoupons;
import xyz.chenyongpeng.utils.PagesData;
import xyz.chenyongpeng.utils.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Coupon")
public class CouponController {

    @Autowired
    private UserService userService;

    @Autowired
    private CouponService couponService;
    /**
     * 获取所有优惠券
     * @param wx_openid
     * @param include_goods
     * @param limit
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestParam(name = "wx_openid",required = true) String wx_openid,
                  @RequestParam(name = "include_goods",required = false) String include_goods,
                  @RequestParam(name = "limit",required = false) String limit){

        if (StringUtils.isEmpty(wx_openid)){
            return R.error(403, "没有登录");
        }

        if (StringUtils.isEmpty(limit)){
            limit = "0,4";
        }

        Map<String,Object> result = new HashMap<>();
        String u_id = userService.getUserIdByOpenid(wx_openid);
        List<String> couponIds = new ArrayList<>();
        List<ZsCouponIncludeGoods> zsCouponIncludeGoodsList = new ArrayList<>();
        if (!StringUtils.isEmpty(include_goods)){
            zsCouponIncludeGoodsList = couponService.getCouponIdByGoodsId(include_goods);
            if (zsCouponIncludeGoodsList == null || zsCouponIncludeGoodsList.size() == 0){
                result.put("data", new PagesData(0,null));
                return R.ok(200,"没有更多的优惠券了！",result);
            }else{
                for (ZsCouponIncludeGoods zs: zsCouponIncludeGoodsList) {
                    couponIds.add(zs.getCouponId()+"");
                }
            }
        }

        List<String> coupons = couponService.getAllUserCoupon(u_id);
        Map<String,Object> couponsParms = new HashMap<>();
        couponsParms.put("IncouponsId", couponIds);
        couponsParms.put("NotInCouponsId", coupons);
        couponsParms.put("limit", limit);
        List<CouponTo> couponTos = couponService.getListByLimit(couponsParms);
        int couponTosCount = couponService.countByCoupon(couponsParms);

        if (couponTos.size() == 0){
            return R.error(200, "没有更多的优惠券了！");
        }else {
            result.put("data", new PagesData(couponTosCount, couponTos));
            return R.ok();
        }
    }


    /**
     * 领取一张优惠券
     * @param wx_openid
     * @param coupon_id
     * @return
     */
    @PostMapping("/get")
    public R get(@RequestParam(name = "wx_openid",required = true) String wx_openid,
                 @RequestParam(name = "coupon_id",required = true) String coupon_id){
        if (StringUtils.isEmpty(wx_openid)){
            return R.error(403, "没有登录");
        }
        if (StringUtils.isEmpty(coupon_id)){
            return R.error(500, "缺少参数");
        }

        String u_id = userService.getUserIdByOpenid(wx_openid);
        if (StringUtils.isEmpty(u_id)){
            return R.error(500, "没有这个用户");
        }

        boolean flag = couponService.publish(coupon_id,u_id);
        if (flag){
            return R.ok();
        }else {
            return R.error(500, "未知错误");
        }
    }


    /**
     * 获取一个用户所有优惠券
     * @param wx_openid
     * @param limit
     * @return
     */
    @PostMapping("/myList")
    public R myList(@RequestParam(name = "wx_openid",required = true) String wx_openid,
                 @RequestParam(name = "limit",required = false) String limit){
        if (StringUtils.isEmpty(wx_openid)){
            return R.error(403, "没有登录");
        }
        String u_id = userService.getUserIdByOpenid(wx_openid);
        if (StringUtils.isEmpty(u_id)){
            return R.error(500, "没有这个用户");
        }
        if (StringUtils.isEmpty(limit)){
            limit = "0,4";
        }
        Map<String,Object> parms = new HashMap<>();
        parms.put("u_id", u_id);
        parms.put("limit", limit);
        List<MyCoupons> list = couponService.getMyCoupons(parms);
        int count = couponService.getMyCouponsCount(parms);

        if (list.size() != 0){
            Map<String,Object> result = new HashMap<>();
            result.put("data", new PagesData(count, list));
            return R.ok(result);
        }else {
            return R.error(400, "没有更多的优惠券了！");
        }
    }


    /**
     * 优惠券详情
     * @param wx_openid
     * @param code_id
     * @return
     */
    @PostMapping("/details")
    public R details(@RequestParam(name = "wx_openid",required = true) String wx_openid,
                    @RequestParam(name = "code_id",required = true) String code_id){
        if (StringUtils.isEmpty(wx_openid)){
            return R.error(403, "没有登录");
        }
        if (StringUtils.isEmpty(code_id)){
            return R.error(500, "参数缺少");
        }
        String u_id = userService.getUserIdByOpenid(wx_openid);
        if (StringUtils.isEmpty(u_id)){
            return R.error(500, "没有这个用户");
        }
        Map<String,Object> parms = new HashMap<>();
        parms.put("code_id", code_id);
        List<MyCoupons> list = couponService.getMyCoupons(parms);
        if (list.size()!=0){
            String coupon_id = String.valueOf(list.get(0).getCoupon_id());
            List<Long> goods_id = couponService.getGoodsIdByCouponId(coupon_id);
            MyCouponInfo myCouponInfo = new MyCouponInfo();
            myCouponInfo.setInclude_goods(goods_id);
            BeanUtils.copyProperties(list.get(0), myCouponInfo);
            Map<String,Object> result = new HashMap<>();
            result.put("data", myCouponInfo);
            return R.ok(result);
        }else {
            return R.error(500, "未知错误");
        }
    }
}
